`
https://leetcode.cn/problems/minimum-number-of-days-to-make-m-bouquets/
`

/**
 * @param {number[]} bloomDay
 * @param {number} m
 * @param {number} k
 * @return {number}
 */
var minDays = function (bloomDay, m, k) {
  const n = bloomDay.length
  // 一共需要 m * k 朵花
  if (m * k > n) return -1

  // 第 d 天能否制作 m 束花
  const check = (d) => {
    // 能制作几束花
    let sum = 0
    // 当前连续的开花数量
    let curFlower = 0
    for (const bd of bloomDay) {
      if (d >= bd) {
        curFlower++
        if (curFlower === k) {
          curFlower = 0
          sum++
          if (sum >= m) return true
        }
      } else {
        curFlower = 0
      }
    }
    return false
  }

  let left = Math.min(...bloomDay) - 1, right = Math.max(...bloomDay)
  while (left + 1 < right) {
    const mid = left + Math.floor((right - left) / 2)
    if (check(mid)) {
      right = mid
    } else {
      left = mid
    }
  }
  return right
};